home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GDEVS3GA.C < prev    next >
C/C++ Source or Header  |  1993-05-12  |  8KB  |  242 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevs3ga.c */
  20. /* S3 86C911 driver for Ghostscript */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gxdevice.h"
  24. #include "gdevpcfb.h"
  25. #include "gdevsvga.h"
  26.  
  27. /* Shared routines from gdevsvga.c */
  28. extern int vesa_get_mode(P0());
  29. extern void vesa_set_mode(P1(int));
  30.  
  31. /* Macro for casting gx_device argument */
  32. #define fb_dev ((gx_device_svga *)dev)
  33.  
  34. /* ------ The S3 86C911 device ------ */
  35.  
  36. private dev_proc_open_device(s3_open);
  37. private dev_proc_fill_rectangle(s3_fill_rectangle);
  38. private dev_proc_copy_mono(s3_copy_mono);
  39. private gx_device_procs s3_procs = {
  40.     s3_open,
  41.     gx_default_get_initial_matrix,
  42.     gx_default_sync_output,
  43.     gx_default_output_page,
  44.     svga_close,
  45.     svga_map_rgb_color,
  46.     svga_map_color_rgb,
  47.     s3_fill_rectangle,
  48.     gx_default_tile_rectangle,
  49.     s3_copy_mono,
  50.     svga_copy_color,    /****** DOESN'T WORK ******/
  51.     gx_default_draw_line,
  52.     svga_get_bits,        /****** DOESN'T WORK ******/
  53.     gx_default_get_props,
  54.     gx_default_put_props
  55. };
  56. gx_device_svga gs_s3vga_device =
  57.     svga_device(s3_procs, "s3vga", vesa_get_mode, vesa_set_mode, NULL);
  58.  
  59. /* Keep track of the character bitmap cache in off-screen memory. */
  60. #define log2_cell_width 5
  61. #define cell_width (1 << log2_cell_width)
  62. #define cache_x_bits (log2_cache_width_bits - log2_cell_width)
  63. #define log2_cell_height 5
  64. #define cell_height (1 << log2_cell_height)
  65. #define cache_y_bits (log2_cache_height - log2_cell_height)
  66. #define log2_cache_width_bits 10
  67. #define log2_cache_width_bytes (log2_cache_width_bits - 3)
  68. #define log2_cache_height 8
  69. #define log2_cache_capacity (cache_x_bits + cache_y_bits)
  70. #define cache_capacity (1 << log2_cache_capacity)
  71. private gx_bitmap_id cache_ids[cache_capacity];
  72.  
  73. /* Define additional registers and I/O addresses. */
  74. #define crtc_addr 0x3d4        /* (color) */
  75. #define crt_lock 0x35
  76. #define crt_s3_lock1 0x38
  77. #define crt_s3_lock2 0x39
  78. #define s3_y_pos 0x82e8
  79. #define s3_x_pos 0x86e8
  80. #define s3_y_dest 0x8ae8
  81. #define s3_x_dest 0x8ee8
  82. #define s3_width 0x96e8
  83. #define s3_status 0x9ae8    /* read only */
  84. #define s3_command 0x9ae8    /* write only */
  85. #define s3_back_color 0xa2e8
  86. #define s3_fore_color 0xa6e8
  87. #define s3_write_mask 0xaae8
  88. #define s3_read_mask 0xaee8
  89. #define s3_back_mix 0xb6e8
  90. #define s3_fore_mix 0xbae8
  91. #define s3_height 0xbee8
  92. #define s3_mf_control 0xbee8
  93. #  define mf_data_ones 0xa000
  94. #  define mf_data_cpu 0xa080
  95. #  define mf_data_display 0xa0c0
  96. #define s3_pixel_data 0xe2e8
  97. /* Wait for the command FIFO to empty. */
  98. #define s3_wait_fifo()\
  99.   while ( inport(s3_status) & 0xff )
  100. /* Load the parameters for a rectangle operation. */
  101. #define out_s3_rect(x, y, w, h)\
  102.   (outport(s3_x_pos, x), outport(s3_y_pos, y),\
  103.    outport(s3_width, (w) - 1), outport(s3_height, (h) - 1))
  104.  
  105. private int
  106. s3_open(gx_device *dev)
  107. {    static const mode_info mode_table[] = {
  108.        {     640,  480, 0x201    },
  109.        {     800,  600, 0x203    },
  110.        {    1024,  768, 0x205    },
  111.        {    -1, -1, -1    }
  112.     };
  113.     int code = svga_find_mode(dev, mode_table);
  114.     if ( code < 0 ) return_error(gs_error_rangecheck);
  115.     /* The enhanced modes all use a 1024-pixel raster. */
  116.     fb_dev->raster = 1024;
  117.     code = svga_open(dev);
  118.     if ( code < 0 ) return code;
  119.     /* Clear the cache */
  120.     {    int i;
  121.         for ( i = 0; i < cache_capacity; i++ )
  122.             cache_ids[i] = gx_no_bitmap_id;
  123.     }
  124.     return 0;
  125. }
  126.  
  127. /* Fill a rectangle. */
  128. int
  129. s3_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  130.   gx_color_index color)
  131. {    fit_fill(dev, x, y, w, h);
  132.     s3_wait_fifo();
  133.     outport(s3_fore_mix, 0x27);
  134.     outport(s3_fore_color, (int)color);
  135.     outport(s3_mf_control, mf_data_ones);
  136.     out_s3_rect(x, y, w, h);
  137.     outport(s3_command, 0x40b3);
  138.     return 0;
  139. }
  140.  
  141. /* Copy a monochrome bitmap.  The colors are given explicitly. */
  142. /* Color = gx_no_color_index means transparent (no effect on the image). */
  143. private int
  144. s3_copy_mono(gx_device *dev,
  145.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  146.   int x, int y, int w, int h, gx_color_index czero, gx_color_index cone)
  147. {    int sbit;
  148.     const byte *sptr;
  149.     int run;
  150.     byte lmask;
  151.     byte lmerge = 0;
  152.     int cache_index, cache_x, cache_y;
  153.     int i, j;
  154.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  155.     sbit = sourcex & 7;
  156.     sptr = base + (sourcex >> 3);
  157.     run = (sbit + w + 7) >> 3;
  158.     lmask = 0xff >> sbit;
  159.     /* See whether the cache is applicable. */
  160.     if ( id != gx_no_bitmap_id && w <= cell_width - 7 &&
  161.          h <= cell_height
  162.        )
  163.     {    cache_index = (int)(id & (cache_capacity - 1));
  164.         cache_x = ((cache_index & ((1 << cache_x_bits) - 1)) <<
  165.             log2_cell_width) + 7;
  166.         cache_y = ((cache_index >> cache_x_bits) <<
  167.             log2_cell_height) + 768;
  168.         if ( cache_ids[cache_index] != id )
  169.         {    cache_ids[cache_index] = id;
  170.             /* Copy the bitmap to the cache. */
  171.             s3_wait_fifo();
  172.             out_s3_rect(cache_x - sbit, cache_y, w + sbit, h);
  173.             outport(s3_fore_mix, 0x22);    /* 1s */
  174.             outport(s3_back_mix, 0x01);    /* 0s */
  175.             outport(s3_mf_control, mf_data_cpu);
  176.             outport(s3_command, 0x41b3);
  177.             {    const int skip = raster - run;
  178.                 for ( i = h; i > 0; i--, sptr += skip )
  179.                   for ( j = run; j > 0; j--, sptr++ )
  180.                     outportb(s3_pixel_data, *sptr);
  181.             }
  182.         }
  183.         s3_wait_fifo();
  184.     }
  185.     else
  186.     {    cache_index = -1;
  187.         if ( lmask != 0xff )
  188.         {    /* The hardware won't do the masking for us. */
  189.             if ( czero != gx_no_color_index )
  190.             {    if ( cone != gx_no_color_index )
  191.                 {    s3_fill_rectangle(dev, x, y, w, h, czero);
  192.                     czero = gx_no_color_index;
  193.                 }
  194.                 else
  195.                 {    lmerge = ~lmask;
  196.                 }
  197.             }
  198.         }
  199.         s3_wait_fifo();
  200.         out_s3_rect(x - sbit, y, w + sbit, h);
  201.     }
  202.     /* Load the colors for the real transfer. */
  203.     if ( cone != gx_no_color_index )
  204.     {    outport(s3_fore_mix, 0x27);
  205.         outport(s3_fore_color, (int)cone);
  206.     }
  207.     else
  208.         outport(s3_fore_mix, 0x63);
  209.     if ( czero != gx_no_color_index )
  210.     {    outport(s3_back_mix, 0x07);
  211.         outport(s3_back_color, (int)czero);
  212.     }
  213.     else
  214.         outport(s3_back_mix, 0x63);
  215.     s3_wait_fifo();
  216.     if ( cache_index < 0 )        /* direct transfer */
  217.     {    outport(s3_mf_control, mf_data_cpu);
  218.         outport(s3_command, 0x41b3);
  219.         if ( run == 1 && !lmerge )    /* special case for chars */
  220.         {    for ( i = h; i > 0; i--, sptr += raster )
  221.                 outportb(s3_pixel_data, *sptr & lmask);
  222.         }
  223.         else
  224.         {    const int skip = raster - run;
  225.             for ( i = h; i > 0; i--, sptr += skip )
  226.             {    outportb(s3_pixel_data, (*sptr++ & lmask) | lmerge);
  227.                 for ( j = run; j > 1; j--, sptr++ )
  228.                     outportb(s3_pixel_data, *sptr);
  229.             }
  230.         }
  231.     }
  232.     else
  233.     {    /* Copy the character from the cache to the screen. */
  234.         out_s3_rect(cache_x, cache_y, w, h);
  235.         outport(s3_x_dest, x);
  236.         outport(s3_y_dest, y);
  237.         outport(s3_mf_control, mf_data_display);
  238.         outport(s3_command, 0xc0b3);
  239.     }
  240.     return 0;
  241. }
  242.